<script>
import commonListFactory from '../components/CommonList'
import { termination } from '@/api/channelTask2/change'
export default {
  name: 'ShopChange',
  data() {
    return {
      tabsConfig: [
        {
          name: 'all',
          label: '全部',
          defaultFiltersRaw: [
            { id: 'type', property: 'type', value: '[C,D]', operator: 'IN' }
          ],
          defaultParamsRaw: {},
          tableConfig: {
            interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerChange/page`,
            tabIndex: 1,
            buttonGroupCode: 'all',
            // 显示顺序：优先显示变更进度值为“已保存”的数据，
            // 后显示变更进度为“已提交”的数据，相同进度的数据按照创建时间倒序排列
            sortersRow: [
              { 'id': 'schedule', 'property': 'schedule', 'direction': 'ASC' },
              { 'id': 'createTime', 'property': 'createTime', 'direction': 'DESC' }
            ],
            setting: [
              {
                prop: 'createTime',
                label: '创建时间',
                minWidth: 140
              }
            ],
            btnList: process.env.NODE_ENV === 'development' ? [
              { buttonName: '新增业务', optCode: 'add', outerFlag: true },
              { buttonName: '取消业务', optCode: 'subtract', outerFlag: true }
            ] : null
          }
        },
        {
          name: 'save',
          label: '已保存',
          defaultFiltersRaw: [
            { id: 'type', property: 'type', value: '[C,D]', operator: 'IN' },
            { id: 'schedule', property: 'schedule', value: 'A', operator: '=' }
          ],
          defaultParamsRaw: {},
          tableConfig: {
            interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerChange/page`,
            tabIndex: 2,
            buttonGroupCode: 'save',
            sortersRow: [
              { 'id': 'createTime', 'property': 'createTime', 'direction': 'DESC' }
            ],
            setting: [
              {
                prop: 'createTime',
                label: '创建时间',
                minWidth: 140
              }
            ],
            btnList: process.env.NODE_ENV === 'development' ? [
              { buttonName: '编辑', optCode: 'edit', outerFlag: true },
              { buttonName: '终止', optCode: 'termination', outerFlag: true }
            ] : null
          }
        },
        {
          name: 'audit',
          label: '待审核',
          defaultFiltersRaw: [
            { id: 'type', property: 'type', value: '[C,D]', operator: 'IN' },
            { id: 'schedule', property: 'schedule', value: 'C', operator: '=' }
          ],
          defaultParamsRaw: {},
          tableConfig: {
            interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerChange/page`,
            tabIndex: 3,
            buttonGroupCode: 'audit',
            sortersRow: [
              { 'id': 'createTime', 'property': 'createTime', 'direction': 'DESC' }
            ],
            setting: [
              {
                prop: 'createTime',
                label: '创建时间',
                minWidth: 140
              }
            ],
            btnList: process.env.NODE_ENV === 'development' ? [
              { buttonName: '审核', optCode: 'audit', outerFlag: true }
            ] : null
          }
        }
      ]
    }
  },
  methods: {
    toDetail(row) {
      this.$pageStack.push(`src/views/channelTask2/dealerMarketingInfo/archive/shopChange/components/detail.vue`, {
        props: {
          id: row.id,
          isAdd: row.type === 'C'
        }
      })
    },
    handleBtnOperate(flag, rows) {
      this[flag] && this[flag](rows)
    },
    add(rows) {
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/shopChange/components/AddEdit.vue', {
        props: {
          isAdd: true
        }
      }, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    subtract(rows) {
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/shopChange/components/AddEdit.vue', {
        props: {
          isAdd: false
        }
      }, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    edit(rows) {
      if (!this.singleJudge(rows, '编辑', true)) {
        return
      }
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/shopChange/components/AddEdit.vue', {
        props: {
          id: rows[0].id,
          isAdd: rows[0].type === 'C'
        }
      }, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    termination(rows) {
      if (!this.singleJudge(rows, '终止')) {
        return
      }
      if (rows[0].schedule !== 'A') {
        this.$message.warning('只能终止【已保存】的单据')
        return
      }
      this.$msgbox({
        title: '终止',
        message: '提交后将终止此变更数据，是否确认？',
        showCancelButton: true,
        showConfirmButton: true,
        type: 'warning'
      }).then(() => {
        return termination(rows[0].id).then(res => {
          this.$message.success('操作成功!')
          this.$refs.commonList.refreshTableBoxData('onPage')
        })
      })
    },
    audit(rows) {
      if (!this.singleJudge(rows, '审核', true)) {
        return
      }
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/shopChange/components/Audit.vue', {
        props: {
          id: rows[0].id
        }
      }, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    }
  },
  render(h) {
    return h(commonListFactory({ tabsConfig: this.tabsConfig }), {
      on: {
        btnOperate: this.handleBtnOperate,
        'on-detail': this.toDetail
      },
      ref: 'commonList'
    })
  }
}
</script>
